﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Clock1
{
    public partial class Form1 : Form
    {
        DateTime date = DateTime.Now;
        public Form1()
        {
            InitializeComponent();
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
        }

        private void clockControl11_Load(object sender, EventArgs e)
        {
        }


        private void clockControl11_Paint(object sender, PaintEventArgs e)
        {
            var g = e.Graphics;
            g.Clear(Color.AliceBlue);

            Size size = clockControl11.Size;
            Rectangle rec = new Rectangle(new Point(0, 0), size);
            g.DrawImage(Properties.Resources.微信图片_20190704212731, rec);

            //移动坐标系
            //把坐标原点移动到长方形中心
            g.TranslateTransform(size.Width / 2, size.Height / 2);

            //取长度最小值
            var min = Math.Min(size.Width, size.Height);

            //画一个圆
            Rectangle clockRec = new Rectangle(-min / 2, -min / 2, min, min);
            g.FillEllipse(new SolidBrush(Color.LightYellow), clockRec);

            //画数字
            g.DrawString("12", new Font("宋体", 10), new SolidBrush(Color.Black), new Point(-5, -min / 2));
            g.DrawString("6", new Font("宋体", 10), new SolidBrush(Color.Black), new Point(-3, min / 2 - 10));
            g.DrawString("3", new Font("宋体", 10), new SolidBrush(Color.Black), new Point(min / 2 - 10, 0));
            g.DrawString("9", new Font("宋体", 10), new SolidBrush(Color.Black), new Point(-min / 2, 0));

            //画针
            var hour = DateTime.Now.Hour;
            if (hour >= 12)
            {
                hour -= 12;
            }
            var minute = DateTime.Now.Minute;
            var second = DateTime.Now.Second;
            var dgreeHour = 360 * hour / 12 + 30 * minute / 60;
            g.RotateTransform(dgreeHour);
            g.DrawLine(new Pen(new SolidBrush(Color.CadetBlue), 5), new Point(0, 0), new Point(0, -min / 4));
            g.RotateTransform(-dgreeHour);

            var dgreeminute = 360 * minute / 60;
            g.RotateTransform(dgreeminute);
            g.DrawLine(new Pen(new SolidBrush(Color.CadetBlue), 2), new Point(0, 0), new Point(0, -min / 3));
            g.RotateTransform(-dgreeminute);

            var dgreesecond = 360 * second / 60;
            g.RotateTransform(dgreesecond);
            g.DrawLine(new Pen(new SolidBrush(Color.LightSalmon), 1), new Point(0, 0), new Point(0, -min / 2));
            g.RotateTransform(-dgreesecond);

        }
    }
}
